//
//  HNSH_RemarkViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/16.
//

#import "HNSH_RemarkViewController.h"

@interface HNSH_RemarkViewController ()

/** */
@property (nonatomic, strong)UIView * bgView;
/** */
@property (nonatomic, strong)UILabel * firLabel;
/** */
@property (nonatomic, strong)UIView * firBgView;
/** */
@property (nonatomic, strong)UITextField * firTextField;
/** */
@property (nonatomic, strong)UIButton * sendBtn;

@end

@implementation HNSH_RemarkViewController

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
        
    [self showNavTitle:@"" showLeftItem:YES];
    [self addAllViews];
    [self addAllLayout];
}

- (void)addAllViews{
    [self.view addSubview:self.bgView];
    [self.bgView addSubview:self.firLabel];
    [self.bgView addSubview:self.firBgView];
    [self.firBgView addSubview:self.firTextField];
    [self.view addSubview:self.sendBtn];
}

- (void)addAllLayout{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(kNavBarAndStatusBarHeight + 8);
        make.height.mas_equalTo(130);
    }];
    [self.firLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(24);
        make.right.mas_equalTo(-24);
        make.top.mas_equalTo(16);
        make.height.mas_equalTo(17);
    }];
    [self.firBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.firLabel.mas_bottom).offset(8);
        make.left.right.mas_equalTo(self.firLabel);
        make.height.mas_equalTo(32);
    }];
    [self.firTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.top.bottom.mas_equalTo(0);
    }];
    [self.sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.right.mas_equalTo(-28);
        make.height.mas_equalTo(48);
        make.top.mas_equalTo(self.bgView.mas_bottom).offset(148);
    }];
    
    [[self.firTextField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UITextField *textField = x;
        if (textField.text.length > 15) {
            textField.text = [textField.text substringToIndex:15];
            NSLog(@"备注15字以内 ==> %@",textField.text);
        }
    }];
}

//MARK: Custom Methods
- (void)sendBtnAction{
    if (!self.rcUserId) {
        return;
    }
    [HNSH_ChatNetServer requestRongCloudFriendshipUpdateWithRcUserId:self.rcUserId display_name:self.firTextField.text ? : @"" success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0    ) {
            [self.view makeToast:@"备注成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: Getter Methods
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor appWhiteLabelColor];
        _bgView.layer.cornerRadius = 8;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (UILabel *)firLabel{
    if (!_firLabel) {
        _firLabel = [UILabel new];
        _firLabel.font = [UIFont systemFontOfSize:12];
        _firLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        _firLabel.text = @"设置备注";
    }
    return _firLabel;
}

- (UIView *)firBgView{
    if (!_firBgView) {
        _firBgView = [UIView new];
        _firBgView.backgroundColor = [UIColor colorWithHexString:@"#FAFAFC"];
        _firBgView.layer.cornerRadius = 4;
        _firBgView.layer.masksToBounds = YES;
        _firBgView.layer.borderColor = [UIColor colorWithHexString:@"#E6E6E6"].CGColor;
        _firBgView.layer.borderWidth = 1;
    }
    return _firBgView;
}

- (UITextField *)firTextField{
    if (!_firTextField) {
        _firTextField = [UITextField new];
        _firTextField.font = [UIFont systemFontOfSize:12];
        _firTextField.textColor = [UIColor appBlack333LabelColor];
        _firTextField.placeholder = @"请输入备注";
    }
    return _firTextField;
}

- (UIButton *)sendBtn{
    if (!_sendBtn) {
        _sendBtn = [UIButton new];
        [_sendBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_sendBtn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        _sendBtn.backgroundColor = [UIColor appGreenColor];
        _sendBtn.layer.cornerRadius = 4;
        _sendBtn.layer.masksToBounds = YES;
        [_sendBtn addTarget:self action:@selector(sendBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}

@end
